package com.qfedu.tower.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qfedu.defect.task.dto.DefectTaskDto;
import com.qfedu.defect.task.pojo.DefectTask;
import com.qfedu.menu.mapper.MenuMapper;
import com.qfedu.menu.pojo.Menu;
import com.qfedu.menu.service.MenuService;
import com.qfedu.tower.dto.TowerDTO;
import com.qfedu.tower.dto.TowerStateDto;
import com.qfedu.tower.mapper.TowerMapper;
import com.qfedu.tower.pojo.Tower;
import com.qfedu.tower.service.TowerService;
import com.qfedu.user.dto.StateDto;
import com.qfedu.user.pojo.User;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 功能：
 * 作者：zwf
 * 日期：2024/6/13 20:47
 */
@Service
public class TowerServiceImpl extends ServiceImpl<TowerMapper, Tower> implements TowerService {

    @Transactional(readOnly = true)
    @Override
    public Page<Tower> getTowers(TowerDTO towerDTO) {
        LambdaQueryWrapper<Tower> wrapper = new LambdaQueryWrapper<>();
        String id = towerDTO.getId();
        wrapper.like(StringUtils.hasLength(id), Tower::getId, id);
        String orders = towerDTO.getOrders();
        wrapper.like(StringUtils.hasLength(orders), Tower::getOrders, orders);
        String position =towerDTO.getPosition();
        wrapper.eq(StringUtils.hasLength(position),Tower::getPosition,position);

        Page<Tower> page = new Page<>(towerDTO.getCurrentPage(), towerDTO.getPageSize());
        return page(page, wrapper);
    }

    @Override
    public boolean saveTower(Tower tower) {
        //先查找是否由重复
       LambdaQueryWrapper<Tower> queryWrapper = new LambdaQueryWrapper<>();
       queryWrapper.like(StringUtils.hasLength(tower.getId()),Tower::getId,tower.getId())
               .or()
               .like(StringUtils.hasLength(tower.getOrders()),Tower::getOrders,tower.getOrders());
        List<Tower> towers = this.baseMapper.selectList(queryWrapper);
        if(towers.size()>0){
            return false;
        }
        return baseMapper.insert(tower)>0;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean changeUseState(TowerStateDto towerStateDto) {
        LambdaUpdateWrapper<Tower> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(Tower::getState, towerStateDto.getState());
        wrapper.eq(Tower::getId, towerStateDto.getId());
        return update(wrapper);
    }
}
